package com.eastfair.projectcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.BulletScreenDTO;
import com.eastfair.projectcore.dto.*;
import com.eastfair.projectcore.entity.FormField;
import com.eastfair.projectcore.service.ConfigAttributeService;
import com.eastfair.projectcore.service.FormFieldService;
import com.eastfair.projectcore.vo.FormFieldVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 表单控制类
 * </p>
 *
 * @author zjn
 * @date 2021-08-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/formField")
@Api(value = "formField", tags = "表单控制类")
public class FormFieldController extends SuperController<FormFieldService, Long, FormField, FormFieldPageQuery, FormFieldSaveDTO, FormFieldUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private ConfigAttributeService configAttributeService;
    @Autowired
    private FormFieldService formFieldService;


    @Override
    public void handlerResult(IPage<FormField> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<FormField> formFieldList = list.stream().map((map) -> {
            FormField formField = FormField.builder().build();
            //TODO 请在这里完成转换
            return formField;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(formFieldList));
    }

    /**
     * 新增表单配置
     *
     * @param formFieldDTO 表单实体
     * @return 是否成功
     */
    @ApiOperation(value = "新增表单配置", notes = "新增表单配置")
    @PostMapping("/saveFormField")
    public R<Boolean> saveFormField(@RequestBody @Validated({FormFieldDTO.VerifyInsert.class}) FormFieldDTO formFieldDTO) {
        return formFieldService.saveFormField(formFieldDTO);
    }

    /**
     * 删除表单配置
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation(value = "删除表单配置", notes = "删除表单配置")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@PathVariable("id") Long id) {
        return R.success(formFieldService.updateById(FormField.builder().id(id).isDeleted(BusinessConstant.DELETE_YES).build()));
    }

    /**
     * 根据表单类型获取表单
     *
     * @param formFieldDTO 表单过滤条件
     * @return 表单列表
     */
    @ApiOperation(value = "根据表单类型获取表单", notes = "根据表单类型获取表单")
    @PostMapping("/getBySubjectType")
    public R<List<FormFieldVO>> getBySubjectType(@RequestBody FormFieldDTO formFieldDTO) {
        return formFieldService.getBySubjectType(formFieldDTO);
    }

    /**
     * 查询某配置类的属性 （内部调用）
     *
     * @param formFieldDTO 表单过滤条件
     * @return 表单列表
     */
    @ApiOperation(value = "根据表单类型获取表单", notes = "根据表单类型获取表单")
    @PostMapping("/getBySubjectTypeInside")
    public R<List<FormField>> getBySubjectTypeInside(@RequestBody FormFieldDTO formFieldDTO) {
        List<FormField> FormFieldList = formFieldService.getBySubjectTypeInside(formFieldDTO);
        return R.success(FormFieldList);
    }

    /**
     * 展商展品观众表单校验
     *
     * @param formFieldCheckDTO 表单列表校验条件实体
     * @return 校验结果
     */
    @ApiOperation(value = "展商展品观众表单校验", notes = "展商展品观众表单校验")
    @PostMapping("/checkFormFieldInfo")
    public R<Boolean> checkFormFieldInfo(@RequestBody @Validated({FormFieldCheckDTO.VerifyFormFieldCheck.class}) FormFieldCheckDTO formFieldCheckDTO) {
        return formFieldService.checkFormFieldInfo(formFieldCheckDTO);
    }

    /**
     * 复制表单
     *
     * @param formFieldDTO 展会id，语言环境，使用主体
     * @return 是否成功
     */
    @ApiOperation(value = "复制表单", notes = "复制表单")
    @PostMapping("/copyFormFieldList")
    public R<Boolean> copyFormFieldList(@RequestBody FormFieldDTO formFieldDTO) {
        return formFieldService.copyFormFieldList(formFieldDTO);
    }

}
